package com.dpteam.controller;

import com.dpteam.bean.ProductInfo;
import com.dpteam.service.ProductService;
import com.dpteam.service.StorageService;
import com.dpteam.util.InputCheckUtil;
import com.dpteam.util.Page;
import com.dpteam.util.PageFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by xwh on 2016/1/11.
 */
@Controller
@RequestMapping("/storage")
public class StorageController {

    @Resource
    private StorageService storageService;

    @Resource
    private ProductService productService;

    private static Logger logger = LoggerFactory.getLogger(GoodsController.class);

    /**
     * 添加入库界面
     */
    @RequestMapping("addStorageHtml")
    public String addStorageHtml() {
        logger.info("addStorageHtml");
        return "addStorage";
    }

    /**
     * 添加一次入库
     */
    @RequestMapping("addStorage")
    public String addStorage(String barcode, int amount) {
        logger.info("添加入库[barcode:{},amount:{}]", barcode, amount);
        // 校验
        boolean check = InputCheckUtil.isBarcode(barcode);
        if (!check) {
            // 校验失败，重新刷新界面
            logger.info("校验失败");
            return "forward:/storage/addStorageHtml";
        }

        boolean result = storageService.addImportProduct(barcode, amount);
        if (result) {
            //添加成功
            return "forward:/storage/addStorageHtml";
        } else {
            //添加失败
            return "error";
        }
    }

    /**
     * 修改库存
     */
    @RequestMapping("updateStorage")
    public String updateStorage(int productId, int productStorage) {
        logger.info("修改库存[id:{}, setPrice:{}]", productId, productStorage);
        boolean result = storageService.updateStorage(productId, productStorage);
        if (result) {
            //修改成功
            return "forward:/storage/findAllStorage";
        } else {
            //修改失败
            return "error";
        }
    }

    /**
     * 查询所有库存（分页）
     *
     * @param currentPage
     * @param map
     * @return
     */
    @RequestMapping("findAllStorage")
    public String findAllStorage(ModelMap map, @RequestParam(defaultValue = "1") int currentPage, @RequestParam(defaultValue = "5") int pageSize) {
        logger.info("findAllStorage");
        Page page = PageFactory.getPage(currentPage, pageSize);
        List<ProductInfo> products = productService.findAllProductPage(page);
        map.put("products", products);
        map.put("page", page);
        return "storage";
    }

    /**
     * 根据条形码查询库存
     *
     * @param barcode
     * @return
     */
    @RequestMapping("findStorageByBarcode")
    public String findStorageByBarcode(String barcode, ModelMap map) {
        logger.info("根据条形码查询库存[barcode:{}]", barcode);
        // 校验
        boolean check = InputCheckUtil.isBarcode(barcode);
        if (!check) {
            // 校验失败，重新刷新界面
            logger.info("校验失败");
            return "forward:/storage/findAllStorage";
        }

        List<ProductInfo> products = new ArrayList<>();
        products.add(productService.findProductByBarcode(barcode));
        Page page = PageFactory.getPage();
        map.put("products", products);
        map.put("page", page);
        return "storage";
    }
}
